package com.lottery.game.runner.v2.manager;

import com.lottery.game.common.exception.BaseException;
import com.lottery.game.core.modules.service.game.GameTypePlayingRelationService;
import com.lottery.game.core.modules.service.game.SysDictService;
import com.lottery.game.runner.v2.GameThreadRunner;
import com.lottery.game.runner.v2.manager.request.GameManageRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2019-03-31 16:17
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : EasySite Technology Chengdu Co. Ltd.
 * @link : com.lottery.game.runner.v2.manager.GameRunnerManagerController
 */
@RestController
@RequestMapping("api/v2/game/manager")
@Slf4j
@Api(tags="后台管理-用户账户相关接口---> 注释 ")
public class GameRunnerManagerController {

    @Autowired
    private GameThreadRunner gameThreadRunner;

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private GameTypePlayingRelationService resourceClient;

    @PostMapping("stop")
    @ApiOperation(
            notes = "停止开奖",
            value = "停止开奖"
    )
    public boolean stop(@RequestBody GameManageRequest request) {
        //this.validateKey(request);
        gameThreadRunner.stop(request.getTicket() + "");
        //更新数据库字段
        //resourceClient.updateGameTypeOpenOrCloseByticket(request.getTicket(),0);
        return true;
    }

    @PostMapping("restart")
    @ApiOperation(
            notes = "重新开奖",
            value = "重新开奖"
    )
    public boolean restart(@RequestBody GameManageRequest request) {
       // this.validateKey(request);
        gameThreadRunner.restart(null, request.getTicket() + "", Integer.parseInt(request.getExpect()), request.getTime());
        return true;
    }

    private void validateKey(GameManageRequest request) {
        if (!request.getKey().equals(sysDictService.findPrivateKey())) {
            throw new BaseException("非法操作");
        }
    }
}
